package com.ag.zlw.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.bean.TbUnit;
import com.ag.common.BaseAction;
import com.ag.common.IDUtil;
import com.ag.zlw.service.UnitService;

/**
 * 物品单位
 * 
 * @author 钟龙伟
 * 
 */

@Scope("prototype")
@Controller("UnitAction")
public class UnitAction extends BaseAction {
	private static final long serialVersionUID = -1478025497648926124L;
	@Resource(name = "UnitService")
	private UnitService service;
	private List<TbUnit> UnitList;
	private String message;
	private String unitId;
	private String unitName;

	TbUnit tbUnit = new TbUnit();

	public String find() {
		try {
			UnitList = service.findAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "main";
	}

	public String delete() {
		tbUnit.setUnitId(unitId);
		try {
			service.delete(tbUnit);
			this.setMessage("delete success!");
		} catch (Exception e) {
			this.setMessage("delete fail!");
		}
		return "delete_success";
	}

	public String add() {
		tbUnit.setUnitName(unitName);
		tbUnit.setA(IDUtil.PRESERVE);
		try {
			service.save(tbUnit);
			this.setMessage("save success!");
		} catch (Exception e) {
			this.setMessage("save fail!");
		}
		return "add_success";
	}

	public String modify() {
		tbUnit.setUnitId(unitId);
		tbUnit.setUnitName(unitName);
		tbUnit.setA(IDUtil.PRESERVE);
		try {
			service.modify(tbUnit);
			this.setMessage("modify success!");
		} catch (Exception e) {
			this.setMessage("modify fail!");
		}
		return "modify_success";
	}

	public List<TbUnit> getUnitList() {
		return UnitList;
	}

	public void setUnitList(List<TbUnit> unitList) {
		this.UnitList = unitList;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getUnitId() {
		return unitId;
	}

	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}

	public String getUnitName() {
		return unitName;
	}

	public void setUnitName(String unitName) {
		this.unitName = unitName;
	}

}
